package com.msgqer.rchat.client;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 客户端管理器
 * 
 * @author keaizhuzhu@公众号：面试怪圈 ;官网地址：{@link http://www.mianshiguaiquan.com}
 *         这里有更多面试、技术、学习资料及源码等
 *
 */
public class ClientManager {
	/**
	 * 维护链接的客户端，其中Key为客户端ID，Value为Client
	 */
	private static final ConcurrentHashMap<String, Client> CLIENTS = new ConcurrentHashMap<String, Client>();

	/**
	 * 添加客户端到管理器中
	 * 
	 * @param client
	 */
	public static void addClient(Client client) {
		if (client == null) {
			return;
		}

		CLIENTS.put(client.getClientId(), client);
	}

	/**
	 * 从客户端管理器中移除
	 * 
	 * @param client
	 */
	public static void removeClient(Client client) {
		if (client == null) {
			return;
		}
		System.out.println("removeClient:" + client.getClientName());
		CLIENTS.remove(client.getClientId());
	}

	/**
	 * 获取对应的客户端对象
	 * 
	 * @param id
	 * @return
	 */
	public static Client get(String id) {
		return CLIENTS.get(id);
	}

	/**
	 * 获取连接的客户端
	 * 
	 * @return
	 */
	public static Map<String, Client> getConnectedClients() {
		return CLIENTS;
	}

}
